package cn.easybuy.service.product;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import cn.easybuy.dao.Product_categoryDao;
import cn.easybuy.dao.impl.Product_categoryDaoImpl;
import cn.easybuy.entity.Product_category;
import cn.easybuy.utils.DataSourceUtil;

public class Product_CategoryServiceImpl implements Product_CategoryService {

	//获取1/2/3级菜单
	@Override
	public List<List<Product_category>> getList() {
		Connection conn = DataSourceUtil.getConnection();
		Product_categoryDao pcdao=new Product_categoryDaoImpl(conn);
		List<List<Product_category>> list=new ArrayList<List<Product_category>>();
		list.add(pcdao.getList("1"));
		list.add(pcdao.getList("2"));
		list.add(pcdao.getList("3"));
		return list;
	}

//	public static void main(String[] args){
//		Product_CategoryService pcs=new Product_CategoryServiceImpl();
//		List<List<Product_category>> list=pcs.getList();
//		System.out.println(list.get(0).get(0).getName());
//		for(List<Product_category> pclist : list){
//			for(Product_category pc : pclist){
//				System.out.println("我是"+pc.getType()+"级菜单，我的内容是："+pc.getName());
//			}
//		}
//	}
	@Override
	public int delCategorys(String id) {
		Connection conn = DataSourceUtil.getConnection();
		Product_categoryDao pcdao = new Product_categoryDaoImpl(conn);
		int num=pcdao.checkChildCategory(id);
		if(num>0){
			//分类下存在子分类 返回-1 无法删除
			return -1;
		}else{
			//查询该分类下是否存在商品 如果存在商品 则无法删除
			ProductService ps = new ProductServiceImpl();
			int i = ps.findProductToCategoryId(id);
			if(i!=0){
				return -2;
			}else{
				//执行删除 返回删除行数
				return pcdao.delCategorys(id);
			}			
		}
	}

	@Override
	public int addCategory(Product_category pc) {
		// TODO Auto-generated method stub
		Connection conn = DataSourceUtil.getConnection();
		Product_categoryDao pcdao = new Product_categoryDaoImpl(conn);
		return pcdao.addCategory(pc);
	}	

	@Override
	public Product_category getProductCategoryById(String id) {
		Connection conn = DataSourceUtil.getConnection();
		Product_categoryDao pcdao = new Product_categoryDaoImpl(conn);
		return pcdao.getProductCategoryById(id);
	}

	@Override
	public int updateCategory(Product_category pc) {
		Connection conn = DataSourceUtil.getConnection();
		Product_categoryDao pcdao = new Product_categoryDaoImpl(conn);
		return pcdao.updateCategory(pc);
	}

	@Override
	public List<Product_category> getCategoryList(int pageNo, int pageSize) {
		Product_categoryDao pcdao = new Product_categoryDaoImpl(DataSourceUtil.getConnection());
		return pcdao.getCategoryList(pageNo, pageSize);
	}

	@Override
	public int getCount() {
		Product_categoryDao pcdao = new Product_categoryDaoImpl(DataSourceUtil.getConnection());
		return pcdao.getCount();
	}

}
